<template>
  <div id="chart-container2"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'PieChart2',
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeUnmount() {
    window.removeEventListener('resize', this.resizeChart);
    if (this.myChart) {
      this.myChart.dispose();
    }
  },
  methods: {
    initChart() {
      const dom = document.getElementById('chart-container2');
      this.myChart = echarts.init(dom, null, {
        renderer: 'canvas',
        useDirtyRect: false
      });

      // Calculate total value
      const data = [
        {value: 411, name: '培育科技人才'},
        {value: 467, name: '推动科技应用'},
        {value: 41, name: '培养创科文化'},
        {value: 81, name: '支持研究及发展'},
      ];
      const total = data.reduce((sum, item) => sum + item.value, 0);

      const option = {
        title: {
          top: '2%',
          bottom: '2%',
          text: '二/三级学科项目占比',
          left: 'left',
        },
        legend: {
          bottom: '0%',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: (params) => {
            const percent = ((params.value / total) * 100).toFixed(2);
            return `${params.name}: ${percent}%`;
          }
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['38%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: true,
              position: 'outside',
              formatter: '{d}%', // 仅显示百分比
              fontSize: 11 // 调整字体大小
            },
            labelLine: {
              show: true
            },
            emphasis: {
              label: {
                fontSize: 11,
                fontWeight: 'bold'
              }
            },
            data: data,
            animation: true // 确保启用动画
          }
        ],
        graphic: [
          {
            type: 'text',
            top: '145',
            left: 'center',
            style: {
              text: '累计核准项目',
              textAlign: 'center',
              fill: '#2E75C9',
              font: 'bold 14px sans-serif'
            }
          },
          {
            type: 'text',
            top: '170',
            left: 'center',
            style: {
              text: '73 066',
              textAlign: 'center',
              fill: '#009574', // 可以设置不同的颜色
              font: 'bold 28px sans-serif'
            },
            z: 1
          }
        ]
      };

      if (option && typeof option === 'object') {
        this.myChart.setOption(option);
      }
    },
    resizeChart() {
      if (this.myChart) {
        this.myChart.resize();
      }
    },
    startAnimation() {
      if (this.myChart) {
        this.myChart.setOption(this.myChart.getOption(), true);
      }
    }
  }
};
</script>

<style scoped>
#chart-container2 {
  position: relative;
  width: 20vw;
  height: 24vw;
  background: url("@/assets/card3-3.png"); /* 添加背景图片 */
  background-size: cover; /* 确保背景图片覆盖整个元素 */
  background-repeat: no-repeat; /* 防止背景图片重复 */
  background-position: center; /* 背景图片居中显示 */
}
</style>
